package com.ydev.lazywallet.gui;

import java.util.Iterator;

import org.Constants;
import org.Global;
import org.TempVariables;
import org.ydClipBoardManager;
import org.json.JSONException;
import org.json.JSONObject;

import com.ydev.lazywallet.R;

import CustomUI.HomeTableCell;
import CustomUI.Input_Dialog;
import CustomUI.ViewCellCell;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

public class ViewCell extends Activity {

	LinearLayout llMain;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_cell);

		llMain = (LinearLayout) findViewById(R.id.llTableMain);

		populateCells();
	}

	public void AddClicked(View v) {
		Input_Dialog iD = new Input_Dialog(this, 1);
		iD.show();
	}

	public void ClearClipBoard(View v) {
		ydClipBoardManager.clearClipBoard();
	}

	private void populateCells() {
		JSONObject data;
		JSONObject d;
		try {
			data = new JSONObject(Global.sampleData);
			d = data.optJSONObject(TempVariables.SelectedHomeCell);

			Iterator<String> iter = d.keys();
			ViewCellCell htc;
			
			llMain.removeAllViews();
			
			while (iter.hasNext()) {
				String key = iter.next();
				try {
					Object value = d.get(key);
					htc = new ViewCellCell(this, getApplicationContext());
					htc.setKeyValue(key.toString(), value.toString());
					llMain.addView(htc);
				} catch (JSONException e) {
					// Something went wrong!
				}
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void addObject(String key, String value) {
		JSONObject data;
		JSONObject d;
		try {

			data = new JSONObject(Global.sampleData);
			d = data.optJSONObject(TempVariables.SelectedHomeCell);

			d.put(key, value);
			data.put(TempVariables.SelectedHomeCell, d);
			Global.sampleData = data.toString();
			Global.saveData();
			populateCells();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
